package com.ruoyi.yl.controller;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.yl.common.Result;
import com.ruoyi.yl.domain.YlOutDrug;
import com.ruoyi.yl.domain.YlStockOut;
import com.ruoyi.yl.service.IYlStockOutService;
import com.ruoyi.yl.service.OutManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import static com.ruoyi.yl.constant.YlConstant.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;

/**
 * @Author: linKe
 * @Date: 2022/2/17 17:24
 * @FileName: OutManageController
 * @version: 1.0
 * @Description: 出库管理
 */

@RestController
@RequestMapping("/outManage")
public class OutManageController extends BaseController {

    @Resource
    private OutManageService outManageServiceImpl;
    @Autowired
    private IYlStockOutService ylStockOutService;

    /**
     * 查询出库信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(YlStockOut ylStockOut)
    {
        startPage();
        List<YlStockOut> list = ylStockOutService.selectYlStockOutList(ylStockOut);
        return getDataTable(list);
    }

    /**
     * 导出出库信息列表
     */
    @Log(title = "出库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YlStockOut ylStockOut)
    {
        List<YlStockOut> list = ylStockOutService.selectYlStockOutList(ylStockOut);
        ExcelUtil<YlStockOut> util = new ExcelUtil<YlStockOut>(YlStockOut.class);
        util.exportExcel(response, list, "出库信息数据");
    }

    /**
     * 获取出库信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ylStockOutService.selectYlStockOutById(id));
    }

    /**
     * 新增出库信息
     */
    @Log(title = "出库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YlStockOut ylStockOut)
    {
        return toAjax(ylStockOutService.insertYlStockOut(ylStockOut));
    }

    /**
     * 修改出库信息
     */
    @Log(title = "出库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YlStockOut ylStockOut)
    {
        return toAjax(ylStockOutService.updateYlStockOut(ylStockOut));
    }

    /**
     * 删除出库信息
     */
    @Log(title = "出库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ylStockOutService.deleteYlStockOutByIds(ids));
    }
    


    /**
     * 查看所有出库信息
     */
    @GetMapping("/getAll")
    public TableDataInfo getAll(){
        startPage();
        List<YlStockOut> all = outManageServiceImpl.getAll();
        return getDataTable(all);
    }




    /**
     * 根据id查出库信息
     * 将所需要的字段整合在一起
     */
    @GetMapping("/getOneById/{id}")
    public Result<YlOutDrug> getOneById(@PathVariable("id")Long id){
        YlOutDrug outDrug=outManageServiceImpl.getOneById(1L);
        return new Result<>(OP_SUCCESS_CODE,OP_SUCCESS_MSG,outDrug);
    }
    /**
     * 根据入库类型查询出库信息
     */
    @GetMapping("/getOutsByType/{id}")
    public Result<List<YlStockOut>> getOutsByType(@PathVariable("id")Long id){
        List<YlStockOut> outsByType = outManageServiceImpl.getOutsByType(id);
        return new Result<>(OP_SUCCESS_CODE,OP_SUCCESS_MSG,outsByType);
    }
    /**
     * 根据审核状态查询出库信息
     */
    @GetMapping("/getOutsByCheckStatus/{id}")
    public Result<List<YlStockOut>> getOutsByCheckStatus(@PathVariable("id")Long id){
        List<YlStockOut> outsByType = outManageServiceImpl.getOutsByCheckStatus(id);
        return new Result<>(OP_SUCCESS_CODE,OP_SUCCESS_MSG,outsByType);
    }
    /**
     * 根据输入框模糊查询出库信息
     */
    @GetMapping("/getOutsByLike")
    public Result<List<YlStockOut>> getOutsByLike(String dsc){
        System.out.println(dsc);
        List<YlStockOut> list = outManageServiceImpl.getOutsByLike(dsc);
        return  new Result<>(OP_SUCCESS_CODE,OP_SUCCESS_MSG,list);
    }

    /**
     * 添加出库表对象
     * @param out
     */
    @PostMapping("/addOut")
    public int addOut(YlStockOut out){
        long currentTimeMillis = System.currentTimeMillis();

        String rk="yl"+currentTimeMillis;

        StringBuilder str=new StringBuilder();
        //定义变长字符串
        Random random=new Random();
        //随机生成bai数字du，并添加zhi到字符串
        for(int i=0;i<8;i++){
            str.append(random.nextInt(10));
        }
        //将字dao符串转换为数字并输出
        Long batchNumber=Long.parseLong(str.toString());

        YlStockOut out1 = (YlStockOut)new YlStockOut().setNumber(rk)
                .setOutDate(LocalDate.now())
                .setOutWorker(1L)
                .setOutType(1)
                .setTableMaker(1L)
                .setCheckStatus(1)
                .setCheckDate(LocalDate.of(2022, 2, 21))
                .setCheckWorker(1L)
                .setBatchNumber(str.toString())
                .setPurchaseSum(new BigDecimal(221.2))
                .setSaleSum(new BigDecimal(223.4))
                .setRemark("测试数据");
        return outManageServiceImpl.addOut(out1);
    }
}
